package com.xqboss.common.utils.uuid;

import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Administrator
 * @Description: 生成六位邀请码
 */
public class InvitCodeUtil {
    private static final String CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final int CODE_LENGTH = 6;

    private static SecureRandom random = new SecureRandom();

    public static String generateInviteCode() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < CODE_LENGTH; i++) {
            int randomIndex = random.nextInt(CHARACTERS.length());
            char randomChar = CHARACTERS.charAt(randomIndex);
            sb.append(randomChar);
        }
        return sb.toString();
    }

    public static String generateUniqueInviteCode() {
        Set<String> inviteCodes = new HashSet<>();
        String inviteCode;
        do {
            inviteCode = generateInviteCode();
        } while (!inviteCodes.add(inviteCode));
        return inviteCode;
    }

}
